package com.xxd.gulimail.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xxd.common.validator.group.AddGroup;
import com.xxd.common.validator.group.UpdataGroup;
import com.xxd.common.validator.group.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xxd.gulimail.product.entity.BrandEntity;
import com.xxd.gulimail.product.service.BrandService;
import com.xxd.common.utils.PageUtils;
import com.xxd.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author xxd
 * @email 2132862687@qq.com
 * @date 2022-09-26 10:32:57
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * @Valid 告诉Springmvc这个参数开启了参数校验
     * BindingResult :获取校验错误结果
     */
    @RequestMapping("/save")
    public R save(@Validated(value = {AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/) {
//        if(result.hasErrors()){//获取校验错误结果，封装成R返回
//            HashMap<Object, Object> map = new HashMap<>();
//            List<FieldError> fieldErrors = result.getFieldErrors();
//            for (FieldError fieldError : fieldErrors) {
//                String field = fieldError.getField();
//                map.put(field,fieldError.getDefaultMessage());
//            }
//            return R.error(400,"请求参数不合法").put("data",map);
//        }else {
            brandService.save(brand);
            return R.ok();
//        }
    }

    /**
     * 修改
     * @Validated 开启分组校验
     */
    @RequestMapping("/update")
    public R update(@Validated(value = {UpdataGroup.class})@RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);
        return R.ok();
    }

    @RequestMapping("/update/status")
    public R updateStatus(@Validated(value = {UpdateStatusGroup.class})@RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete( @RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
